package com.sugon.modules.excel.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.collection.CollectionUtil;
import com.sugon.dao.SysRoleDao;
import com.sugon.entity.SysRoleEntity;
import com.sugon.entity.SysUserEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.modules.excel.dto.SysUserImportParamDTO;
import com.sugon.service.InspectionInstitutionService;
import com.sugon.service.SpecialDevHoistingEquService;
import com.sugon.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户Excel导入Handler
 *
 * @author YuXD
 * @since 2023-02-24
 */
@Service("userExcelHandler")
public class UserExcelHandler extends AbstractExcelHandler implements IExcelHandler {

    @Autowired
    private SpecialDevHoistingEquService hoistingEquService;
    @Autowired
    private InspectionInstitutionService inspectionInstitutionService;
    @Resource
    private SysUserService userService;
    @Resource
    private SysRoleDao roleDao;

    /**
     * @param excelInputStream excel输入流
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int importExcelFromStream(InputStream excelInputStream) throws Exception {
        // 获取导入配置
        ImportParams importParams = getImportParams();
        // 从上传文件中读取起重设备台账列表
        List<SysUserImportParamDTO> hoistingEquAccountList = ExcelImportUtil.importExcel(excelInputStream, SysUserImportParamDTO.class, importParams);
        if (CollectionUtil.isEmpty(hoistingEquAccountList)) {
            return 0;
        }
        int importRows = 0;

        SysUserEntity ermsUser = null;
        List<String> existsUserList = CollectionUtil.newArrayList();
        for (SysUserImportParamDTO param : hoistingEquAccountList) {
            // 尝试获取设备信息
            String userName = param.getUserName();
            // 目前只支持导入，不支持更新
            ermsUser = userService.queryByUserName(userName);
            if (ermsUser != null) {
                existsUserList.add(param.getNickName());
                continue;
            }
            ermsUser = new SysUserEntity();
            ermsUser.setUserName(param.getUserName());
            ermsUser.setDeptId(param.getDeptId());
            ermsUser.setSpecial(0);
            ermsUser.setNickName(param.getNickName());
            ermsUser.setDeptCode(param.getDeptCode());
            ermsUser.setStatus(1);
            ermsUser.setLeader("是".equals(param.getLeader()) ? 1 : 0);
            // 设置角色
            List<SysRoleEntity> roleList = roleDao.listByRoleCode(param.getRoleCode());
            if (CollectionUtil.isNotEmpty(roleList)) {
                ermsUser.setRoleIdList(roleList.stream().map(SysRoleEntity::getRoleId).collect(Collectors.toList()));
            }
            userService.save(ermsUser);
            importRows += 1;
        }
        System.out.println(existsUserList);
        return importRows;
    }

    /**
     * 设置导入参数
     *
     * @return
     */
    @Override
    public ImportParams getImportParams() {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setKeyIndex(0);
        return params;
    }

}
